package Servlet;

import javax.servlet.*;
import javax.servlet.annotation.WebServlet;
import java.io.*;
import java.util.Enumeration;
import java.util.Map;


@WebServlet(name = "loginServlet", urlPatterns = "/loginServlet")
public class loginServlet implements Servlet {
    private ServletConfig servletConfig;

    @Override
    public void init(ServletConfig config) throws ServletException {

        try {
            System.setOut(new PrintStream(System.out, true, "UTF-8"));
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }

        this.servletConfig = config;
        String servletName = config.getServletName();
        System.out.println("Servlet 实例的名称：" + servletName);
//获取Servlet中所有的初始化参数
        Enumeration<String> initParameterNames =
                config.getInitParameterNames();
        while (initParameterNames.hasMoreElements()) {
            String initParameterName = initParameterNames.nextElement();
//获取Servlet中给定名称的初始化参数
            String initParameterValue =
                    config.getInitParameter(initParameterName);
            System.out.println("Servlet 初始化参数 " + initParameterName + ":"
                    + initParameterValue);
        }
    }

    @Override
    public ServletConfig getServletConfig() {
        return servletConfig;
    }

    @Override
    public void service(ServletRequest servletRequest, ServletResponse
            servletResponse) throws ServletException, IOException {

        String characterEncoding = servletRequest.getCharacterEncoding();
        System.out.println("Servlet 获取字符编码=>" + servletRequest.getCharacterEncoding());
        servletRequest.setCharacterEncoding("UTF-8");
        System.out.println("Servlet 获取字符编码=>" + servletRequest.getCharacterEncoding());

//        System.out.println("开始获取请求参数信息");
//
//        BufferedReader br = servletRequest.getReader();
//        String line = null;
//        while ((line = br.readLine()) != null) {
//            System.out.println(line);
//        }
//        System.out.println("请求参数信息获取完毕");
//        System.out.println("==========");

        System.out.println("开始获取请求中存储的数据");
        Enumeration<String> attributeNames =
                servletRequest.getAttributeNames();
        while (attributeNames.hasMoreElements()) {
            String attributeName = attributeNames.nextElement();
            System.out.println(attributeName + "=>" + servletRequest.getAttribute(attributeName));
        }
        System.out.println("请求中存储的数据获取完毕");
        System.out.println("==========");

        System.out.println("方式一：开始获取请求参数信息");
        Enumeration<String> parameterNames =
                servletRequest.getParameterNames();
        while (parameterNames.hasMoreElements()) {
            String parameterName = parameterNames.nextElement();
            String parameterValue = servletRequest.getParameter(parameterName);
            System.out.println(parameterName + "=>" + parameterValue);
        }
        System.out.println("方式一请求参数信息获取完毕");
        System.out.println("==========");

        System.out.println("方式二：开始获取请求参数信息");
        Map<String, String[]> parameterMap = servletRequest.getParameterMap();
        for (Map.Entry<String, String[]> entry : parameterMap.entrySet()) {
            String parameterName = entry.getKey();
            String[] parameterValues = entry.getValue();
            for (String parameterValue : parameterValues) {
                System.out.println(parameterName + "=>" + parameterValue);
            }
        }
        System.out.println("方式二请求参数信息获取完毕");
        System.out.println("==========");

        System.out.println("请求所使用的上下文数据");
        System.out.println(servletRequest.getServletContext().getContextPath());

        System.out.println("Servlet 处理请求");

//        Servlet 响应信息处理
        System.out.println();
        System.out.println();
        System.out.println("===========================================");
        System.out.println("响应的字符集编码：" +
                servletResponse.getCharacterEncoding());
        servletResponse.setCharacterEncoding("UTF-8");
        System.out.println("响应的字符集编码：" +
                servletResponse.getCharacterEncoding());
        System.out.println("响应的内容类型：" + servletResponse.getContentType());
        servletResponse.setContentType("text/html;charset=utf-8");
        System.out.println("响应的内容类型：" + servletResponse.getContentType());
        PrintWriter writer = servletResponse.getWriter();
        writer.print("登录请求已处理");
        writer.flush();
        writer.close();

    }

    @Override
    public String getServletInfo() {
        return "Login Servlet";
    }

    @Override
    public void destroy() {
        System.out.println("Servlet 销毁");
    }
}